<template>
  <div class="dialog-container">
    <el-dialog
      title="重置密码"
      :visible="dialogVisible"
      append-to-body
      width="30%"
      @close="closeDialog"
    >
      <el-form
        ref="form"
        :model="dataForm"
        class="dialog-form"
        :rules="rules"
        label-width="110px"
        size="mini"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="新密码：" prop="newPass">
              <el-input v-model="dataForm.newPass" type="password" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="确认新密码：" prop="checkPass">
              <el-input v-model="dataForm.checkPass" type="password" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button :loading="saveLoading" size="mini" type="primary" @click="handleSave">确定</el-button>
        <el-button size="mini" @click="closeDialog">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { resetPassword } from '@/api/system/user'
export default {
  name: 'UpdatePasswordDialog',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    ids: {
      type: Array,
      default: null
    }
  },
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.dataForm.checkPass !== '') {
          this.$refs.form.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.dataForm.newPass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      dataForm: {
        newPass: '',
        checkPass: ''
      },
      rules: {
        newPass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      },
      saveLoading: false
    }
  },
  created() {},
  mounted() {},
  methods: {
    handleSave() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.saveLoading = true
          resetPassword({ ids: this.ids, newPass: this.dataForm.newPass, oldPass: '000000' }).then(() => {
            this.$message.success('重置密码成功')
            this.closeDialog()
            this.saveLoading = false
          }).catch(() => {
            this.$message.error('重置密码失败')
            this.saveLoading = false
          })
        }
      })
    },
    closeDialog() {
      this.$emit('closeDialog')
      this.$emit('flushPage')
      this.$refs['form'].clearValidate()
    }
  }
}
</script>

<style lang='scss' scoped>
</style>
